package com.example.controller;

import java.util.List;
import java.util.Map;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.domain.User;
import com.example.result.Result;
import com.example.service.UserService;
import com.example.tool.GsonTool;

/**
 * @author 袁小弘
 * @describe 用户控制层
 * @date 2019-12-31
 */
@RestController
@RequestMapping("/api")
public class UserController {

    @Autowired
    UserService userService;

//	@RequestMapping(value = "/login", method = RequestMethod.POST)
//	public Result login(@RequestBody String json, HttpSession session) {
//		System.out.println("jsons数据：" + json);
//
//		// JSONObject parseObject = JSON.parseObject(json);
//		// Map<String, Object> map=JSON.toJavaObject(parseObject, Map.class);
//		Map<String, Object> map = (Map<String, Object>) JSON.parseObject(json);
//		String userName = (String) map.get("userName");
//		boolean flag = userService.isExist(userName);
//		if (!flag) {
//			return new Result(400);
//		} else {
//			session.setAttribute("user", userName);
//			return new Result(200);
//		}
//	}


    // ================================用户管理=================================================

    /**
     * @return 返回所有用户信息列表
     * @describe 查询所有用户信息
     */
    @RequestMapping(value = "/users", method = RequestMethod.GET)
    public Result queryAll() {

        return userService.queryAll();
    }

    /**
     * @param admin_Id
     * @return 返回该编号的用户信息
     * @describe 根据用户编号查询用户信息
     */
    @RequestMapping(value = "/users/{user_Id}", method = RequestMethod.GET)
    public Result queryUserById(@PathVariable("user_Id") String user_Id) {

        return userService.queryUserById(user_Id);
    }

    /**
     * @param user_Id
     * @return 删除成功则返回一个成功提示，反之则提示失败信息
     * @describe 根据用户编号删除用户信息
     */
    @RequestMapping(value = "/users/{user_Id}", method = RequestMethod.DELETE)
    public Result deleteUserById(@PathVariable("user_Id") String user_Id) {
        return userService.deleteUserById(user_Id);
    }

    /**
     * @param user_Id
     * @return 删除成功则返回一个成功提示，反之则提示失败信息
     * @describe 根据用户编号批量删除用户信息
     */
    @RequestMapping(value = "/users/dels", method = RequestMethod.POST)
    public Result deleteUserByIds(@RequestBody Map<String, String> userIds) {

        return userService.deleteUserByIds(userIds);
    }

    /**
     * @param json
     * @return 添加成功则返回一个成功提示，反之则提示失败信息
     * @describe 添加用户信息
     */
    @RequestMapping(value = "/users", method = RequestMethod.POST)
    public Result addUser(@RequestBody String json) {
        System.out.println(json);
        JSONObject parseObject = JSON.parseObject(json);
//		  JSONObject str = parseObject.getJSONObject("json");
//		  System.out.println(str);
        User user = GsonTool.getGsonInstance().fromJson(parseObject.getString("json"), User.class);
        System.out.println("GSONUSER=====" + user.toString());
        return userService.addUser(user);
    }

    /**
     * @param json
     * @return 添加成功则返回一个成功提示，反之则提示失败信息
     * @describe 通过Excel批量添加用户信息
     */
    @RequestMapping(value = "/users/excels", method = RequestMethod.POST)
    public Result addUserByExcel(@RequestBody String json) {
        System.out.println(json);
        JSONObject parseObject = JSON.parseObject(json);
        List<User> list = JSON.parseArray(parseObject.getString("json"), User.class);

        System.out.println(list);

        return userService.addUserByExcel(list);

    }

    /**
     * @param json
     * @return 更新成功则返回一个成功提示，反之则提示失败信息
     * @describe 更新用户信息
     */
    @RequestMapping(value = "/users", method = RequestMethod.PUT)
    public Result updateUser(@RequestBody String json) {
        System.out.println(json);
        JSONObject parseObject = JSON.parseObject(json);
        // Map<String, Object> map=JSON.toJavaObject(parseObject, Map.class);
        User user = GsonTool.getGsonInstance().fromJson(parseObject.getString("json"), User.class);
        System.out.println(user.toString());
        return userService.updateUser(user);
    }

    /**
     * @param json
     * @return 查询成功则返回一个成功提示，反之则提示失败信息
     * @describe 用户信息模糊查询
     */
    @RequestMapping(value = "/users/serachs", method = RequestMethod.POST)
    public Result serachByInfo(@RequestBody String json) {
        System.out.println(json);
        JSONObject parseObject = JSON.parseObject(json);
        String[] info = parseObject.getString("json").split(",");

        return userService.serachByInfo(info);

    }

    /**
     * @return 查询成功则返回一个成功提示，反之则提示失败信息
     * @describe 查询所有用户账号和ID==>用于密码管理
     */
    @RequestMapping(value = "/users/accounts", method = RequestMethod.GET)
    public Result queryAllUserNameAndId() {
        return userService.queryAllUserNameAndId();
    }


}
